//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2007
//
// 'Postal address items' dialog, 'Cities' page.
//

# include "StdAfx.h"
# include "Resource.h"
# include "PAItems.h"
# include "ListView.h"
# include "Map.h"
# include "ChangePAI.h"
# include "HoluxTypes.h"
# include "PropertyPages.h"
# include "Globals.h"

bool CCreateOrEditCity::OnInit (HWND _hwndFocus) {
	SetWindowText (m_hWnd, m_pCity == NULL ? "Add City" : "City Properties");

	EnableDlgItem (IDOK, false);

	// Name.
	if (m_pCity)
		SetDlgItemText (IDC_TEXT, m_pCity->strName.c_str ());

	// Static.
	SetDlgItemText (IDC_PARENT_ITEMS, "Regions:");

	// Regions.
	m_List.Init (m_hWnd, IDC_LIST, "Regions");
	InitRegionsList (m_List);
	FillRegionsList (m_List, m_pCity ? m_pCity->pRegion : NULL);
	if (m_pCity == NULL)
		SetSelection (m_List, 0);

	return true;
}

void CCreateOrEditCity::OnDestroy () {
	m_List.Destroy ();
}

void CCreateOrEditCity::OnCommand (WORD _wCmd, WORD _wNotifyCode, HWND _hwndCtl) {
	switch (_wCmd) {
		case IDC_TEXT: {
			if (_wNotifyCode == EN_CHANGE)
				EnableDlgItem (IDOK, true);
			return;
		}

		case IDOK: {
			const HWND hText = GetDlgItem (IDC_TEXT);

			// Get text.
			const size_t cLen = ::GetWindowTextLength (hText);
			char * const strName = reinterpret_cast<char *> (alloca (cLen + 1));
			::GetWindowText (hText, strName, cLen + 1);

			// Get country.
			const size_t cSel = ListView_GetSelectionMark (m_List);
			if (cSel == -1)
				break;
			region_t * const pRegion = reinterpret_cast<region_t *> (GetLParam (m_List, cSel));

			if (m_pCity == NULL) {
				// Create new city.
				AddCity (strName, pRegion);
			} else {
				if (! (m_pCity->strName == strName))
					// Rename existing city.
					RenamePAI (m_pCity, strName);

				if (m_pCity->pRegion != pRegion)
					// Assign region for the existing city.
					SetRegion (m_pCity, pRegion);
			}

			EndDialog (0);
			return;
		}

		case IDCANCEL:
			EndDialog (1);
			return;
	}
}

bool CCreateOrEditCity::OnNotify (int _idCtrl, NMHDR * _pInfo) {
	switch (_pInfo->code) {
		case NM_RCLICK:       m_List.PopupMenu_OnHeaderRClick (_pInfo); break;
		case LVN_COLUMNCLICK: m_List.Sort_OnColumnClick       (_pInfo); break;
	}
	return false;
}

////////////////////////////////////////////////////

CCitiesPage::CCitiesPage () :
	CPropertyPages::CPage (IDD_PA_CITIES),
	m_List (true, false)
{}

void CCitiesPage::_UpdateTotalCities () {
	const size_t cCities = m_List.GetCities ();

	// Show cities total number.
	char strTotal [32];
	::sprintf (strTotal, "Total: %d", cCities);
	SetDlgItemText (IDC_TOTAL, strTotal);
}

void CCitiesPage::_EnableBtns (bool _bEnable, bool _bLocatable) {
	EnableDlgItem (IDC_EDIT,   _bEnable);
	EnableDlgItem (IDC_REMOVE, _bEnable);
	EnableDlgItem (IDC_LOCATE, _bEnable && _bLocatable);
}

bool CCitiesPage::OnInit (HWND _hwndFocus) {
	// Buttons.
	_EnableBtns (false, false);

	// Cities.
	m_List.Init (m_hWnd, IDC_LIST);

	// Locate.
	if (g_map.pTypeSet == & g_tsHolux)
		ShowDlgItem (IDC_LOCATE, false);

	return true;
}

void CCitiesPage::OnDestroy () {
	m_List.Destroy ();
}

void CCitiesPage::OnActivatePage (const PSHNOTIFY *) {
	m_List.Activate (false);
	_UpdateTotalCities ();
}

void CCitiesPage::OnCommand (WORD _wCmd, WORD _wNotifyCode, HWND _hwndCtl) {
	switch (_wCmd) {
		case IDC_ADD: {
			if (g_map.Regions.empty ()) {
				WarningMsgBox ("Please create regions first.");
				return;
			}

			CCreateOrEditCity cecd;
			if (cecd.ShowModal (m_hWnd) == 0) {
				m_List.InsertLastCity ();
				_UpdateTotalCities ();
			}

			return;
		}

		case IDC_EDIT:
			m_List.EditSelCity ();
			return;

		case IDC_REMOVE:
			if (! m_List.RemoveSelCity ())
				_EnableBtns (false, false);
			_UpdateTotalCities ();

			return;

		case IDC_LOCATE: {
			m_List.LocateSelCity ();
			return;
		}
	}
}

bool CCitiesPage::OnNotify (int _idCtrl, NMHDR * _pInfo) {
	switch (_pInfo->code) {
		case LVN_ITEMCHANGED: {
			const NMLISTVIEW * const pInfo2 = reinterpret_cast<const NMLISTVIEW *> (_pInfo);
			if (
				(pInfo2->uOldState & LVIS_SELECTED) == 0 &&
				(pInfo2->uNewState & LVIS_SELECTED) != 0
			) {
				const size_t cSel = pInfo2->iItem;
				if (cSel == -1)
					break;

				_EnableBtns (cSel != -1, m_List.IsCityLocatable (cSel));
			}
			break;
		}

		default:
			if (m_List.OnParentNotify (_idCtrl, _pInfo, this))
				return true;
			break;
	}

	return CPage::OnNotify (_idCtrl, _pInfo);
}
